package org.phalanx.game.units;

import java.util.HashMap;

import org.phalanx.map.MapCoordinates;
import org.phalanx.map.PhalanxMap;

public class PhalanxMapUnitMatrix {
	private HashMap<MapCoordinates,PhalanxMapUnit> gameUnitTable;
	private PhalanxMapUnitSet mapUnitSet;
	private PhalanxMap pMap;
	
	public PhalanxMapUnitMatrix(PhalanxMap pMap, PhalanxMapUnitSet uSet){
		gameUnitTable = new HashMap<MapCoordinates, PhalanxMapUnit>();
		setMap(pMap);
		setMapUnitSet(uSet);
	}

	public void setMap(PhalanxMap pMap) {
		this.pMap = pMap;
	}

	public PhalanxMap getMap() {
		return pMap;
	}

	public void setMapUnitSet(PhalanxMapUnitSet mapUnitSet) {
		this.mapUnitSet = mapUnitSet;
	}

	public PhalanxMapUnitSet getMapUnitSet() {
		return mapUnitSet;
	}
	
	public void addUnitAt(MapCoordinates c, PhalanxMapUnit unit){
		
		if (getMapUnitSet().hasUnit(unit)){
			if (getIsInBounds(c)){
				gameUnitTable.put(c, unit);
			}
		}
	}
	
	public void removeUnitAt(MapCoordinates c){
		if (gameUnitTable.containsKey(c)){
			gameUnitTable.remove(c);
		}
	}
	
	public PhalanxMapUnit getUnitAt(MapCoordinates c){
		PhalanxMapUnit unit = null;
		
		if (gameUnitTable.containsKey(c)){
			unit = gameUnitTable.get(c);
		}
		
		return unit;
	}
	
	private boolean getIsInBounds(MapCoordinates c){
		boolean isBounds = false;
		
		if (c.getX() >= 0 && c.getY() >= 0 && c.getX() < pMap.getMapWidth() && c.getY() < pMap.getMapLength()){
			isBounds = true;
		}
		
		return isBounds;
	}
}
